package com.eco
{
	import com.eco.*;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.*;
	import flash.utils.*;
	
	//敌人的管理类，控制敌人的产生，与参数设定
	public class BonusManager extends MovieClip
	{
		private var vecBonus:Vector.<Bonus>;//储存道具类实例的数组
		private var iBonusNum:int;//道具实例个数，为了简化计算而设
		private var bnBonus:Bonus;//道具类实例的引用，简化一些表达式
		private var ftFighter:Fighter;//卫士类的引用，用于获取卫士类的一些参数
		private var vecEnemy:Vector.<Enemy>;//敌人数组引用，用于获取敌人类的参数
		
		public var sprCreateBonusCMD:Sprite;
		
		private var w:int;//记录舞台的宽和高
		private var h:int;
		
		public function BonusManager()
		{
			//道具管理器的构造函数
			
			vecBonus = new Vector.<Bonus>();
			iBonusNum = 0;
			
			sprCreateBonusCMD = new Sprite();
			sprCreateBonusCMD.addEventListener( CreateObjectEvent.CREATE_BONUS, CreateBonus );
			
			addEventListener( Event.ADDED_TO_STAGE, Initialize);
			
			ftFighter = null;
			vecEnemy = null;
		}
		
		public function SetFighter(fighter:Fighter):void
		{
			ftFighter = fighter;
		}
		
		public function SetEnemyVector(ve:Vector.<Enemy>):void
		{
			vecEnemy = ve;
		}
		
		public function GetBonusVector():Vector.<Bonus>
		{
			//返回敌人数组
			return vecBonus;
		}
		
		/*public function CreateBonus( r:int = 5):void
		{
			//创建道具实例函数
			if( iBonusNum>=1 )//测试用道具数量上限
			{
				return;
			}
			bnBonus = new BonusMassUp();//根据 参数创建道具实例
			bnBonus.SetFighter(ftFighter);
			bnBonus.SetEnemyVector(vecEnemy);
			bnBonus.x = Math.floor( Math.random()*(w-150) );
			bnBonus.y = Math.floor( Math.random()*(h-150) );
			vecBonus.push(bnBonus);//将实例存入数组
			iBonusNum = vecBonus.length;//更新道具个数变量
			addChild( bnBonus );
		}*/
		
		private function Initialize(e:Event):void
		{
			//该函数将在ADDED_TO_STAGE事件发生后被触发
			//首先检测stage是否已被赋值，因为只有当本类的对象和父节点有关联时，stage信息才会被传递下来
			if( stage != null )
			{
				//设置EnemyField每X毫秒调用CreateEnemy函数生成新敌人
				//CreateEnemy中包含了限制敌人数量等判断条件
				//setInterval( CreateBonus, 2000 );
				
				//由于某些原因，舞台的长宽可能有变化，而原始长宽也是许多计算的依据
				w = stage.stageWidth;//因此要把初始时舞台的长宽记录下来，
				h = stage.stageHeight;
				
				//移除监听器
				removeEventListener( Event.ADDED_TO_STAGE, Initialize );
				addEventListener( CreateObjectEvent.CREATE_BONUS, CreateBonus );
			}
		}
		
		public function Init():void
		{
			
		}
		
		
		private function CreateBonus(e:CreateObjectEvent):void
		{ 
			switch (e.objectType)
			{
				case Bonus.BONUS_SPEEDUP:
				{
					bnBonus = new BonusSpeedUp();
					break;
				}
				case Bonus.BONUS_SUPERARMOR:
				{
					bnBonus = new BonusSuperArmor();
					break;
				}
				case Bonus.BONUS_BLAST:
				{
					bnBonus = new BonusBlast();
					break;
				}
				case Bonus.BONUS_MASSUP:
				{
					bnBonus = new BonusMassUp();
					break;
				}
				case Bonus.BONUS_EXPLOSIVE:
				{
					bnBonus = new BonusExplosive();
					break;
				}
				case Bonus.BONUS_INVINCIBLE:
				{
					bnBonus = new BonusInvincible();
					break;
				}
			}
			
			bnBonus.SetFighter( ftFighter );
			bnBonus.x = e.posX;
			bnBonus.y = e.posY;
			vecBonus.push(bnBonus);//将实例存入数组
			iBonusNum = vecBonus.length;//更新道具个数变量
			addChild( bnBonus );
			
		}
		
		public function Update(e:Event = null):void
		{
			var i:int = 0;
			for( i=0; i<iBonusNum; )
			{
				vecBonus[i].Update();
				
				switch( vecBonus[i].iState )
				{
					case Bonus.STATE_IDLE:
					{
						i++;
						break;
					}
					case Bonus.STATE_ACTIVE:
					{
						//道具被触发之后就不用显示在场景中了
						//但是考虑到瞬发和持续两类道具的存在，此状态只能够将对象设为不可见
						//移除显示列表与移出数组的操作要在结束状态中执行
						//vecBonus[i].visible = false;
						i++;
						break;
					}
					case Bonus.STATE_END:
					{
						//道具使用结束之后再将其移出显示列表与数组
						removeChild( vecBonus[i] );
						vecBonus.splice( i, 1 );
						iBonusNum --;
						break;
					}
				}
			}
		}
		
		public function Pause():void
		{
			var i:int = 0;
			for( i=0; i<iBonusNum; i++)
			{
				vecBonus[i].Pause();
			}
			
			for( i=0; i<sprCreateBonusCMD.numChildren; i++)
			{
				( sprCreateBonusCMD.getChildAt(i) as CreateBonusCMD ).Pause();
			}
		}
		
		public function Continue():void
		{
			var i:int = 0;
			for( i=0; i<iBonusNum; i++)
			{
				vecBonus[i].Continue();
			}
			
			for( i=0; i<sprCreateBonusCMD.numChildren; i++)
			{
				( sprCreateBonusCMD.getChildAt(i) as CreateBonusCMD ).Continue();
			}
		}
		
		public function Clear():void
		{
			sprCreateBonusCMD.removeEventListener( CreateObjectEvent.CREATE_BONUS, CreateBonus );
			ftFighter = null;
			bnBonus = null;
		}
	}
}